package cn.edu.sgu.www.mhxysy.service.waiguan.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.waiguan.ShizhuangCategoryDTO;
import cn.edu.sgu.www.mhxysy.dto.waiguan.ShizhuangCategoryInsertBatchDTO;
import cn.edu.sgu.www.mhxysy.entity.waiguan.Color;
import cn.edu.sgu.www.mhxysy.entity.waiguan.ShizhuangCategory;
import cn.edu.sgu.www.mhxysy.mapper.waiguan.ColorMapper;
import cn.edu.sgu.www.mhxysy.mapper.waiguan.ShizhuangCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.waiguan.ShizhuangCategoryPager;
import cn.edu.sgu.www.mhxysy.service.waiguan.ShizhuangCategoryService;
import cn.edu.sgu.www.mhxysy.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ShizhuangCategoryServiceImpl implements ShizhuangCategoryService {

    private final ColorMapper colorMapper;
    private final UploadUtils uploadUtils;
    private final ImageUpdater imageUpdater;
    private final ShizhuangCategoryMapper shizhuangCategoryMapper;

    @Autowired
    public ShizhuangCategoryServiceImpl(
            ColorMapper colorMapper,
            UploadUtils uploadUtils,
            ImageUpdater imageUpdater,
            ShizhuangCategoryMapper shizhuangCategoryMapper) {
        this.colorMapper = colorMapper;
        this.uploadUtils = uploadUtils;
        this.imageUpdater = imageUpdater;
        this.shizhuangCategoryMapper = shizhuangCategoryMapper;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String directory = CustomDirectories.DIRECTORY_SZ;

        return uploadUtils.upload(file, directory);
    }

    @Override
    public void insert(ShizhuangCategoryDTO shizhuangCategoryDTO) {
        ShizhuangCategory shizhuangCategory = toEntity(shizhuangCategoryDTO);

        shizhuangCategory.setId(null);
        
        shizhuangCategoryMapper.insert(shizhuangCategory);
    }

    @Override
    public void insertBatch(ShizhuangCategoryInsertBatchDTO insertBatchDTO) {
        Integer parentId = insertBatchDTO.getParentId();
        List<Integer> colorIds = insertBatchDTO.getColorIds();
        ShizhuangCategory selectById = shizhuangCategoryMapper.selectById(parentId);

        // 批量添加数据
        ShizhuangCategory shizhuangCategory = new ShizhuangCategory();

        shizhuangCategory.setIsParent(0);
        shizhuangCategory.setType(selectById.getType());
        shizhuangCategory.setGender(selectById.getGender());
        shizhuangCategory.setFashion(selectById.getFashion());
        shizhuangCategory.setParentId(insertBatchDTO.getParentId());

        // 每个时装类型预留了9个位置，直接设置从parentId开始的自增主键即可
        for (Integer colorId : colorIds) {
            Color color = colorMapper.selectById(colorId);

            shizhuangCategory.setId(++ parentId);
            shizhuangCategory.setName(selectById.getName() + "(" + color.getName() + ")");
            shizhuangCategory.setColorId(colorId);

            shizhuangCategoryMapper.insert(shizhuangCategory);
        }
    }

    @Override
    public void deleteById(Integer id) {
        shizhuangCategoryMapper.deleteById(id);
    }

    @Override
    public void updateById(ShizhuangCategoryDTO shizhuangCategoryDTO) {
        ObjectUtils.requireNonNull(shizhuangCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);
        
        ShizhuangCategory shizhuangCategory = toEntity(shizhuangCategoryDTO);
        
        shizhuangCategoryMapper.updateById(shizhuangCategory);
    }

    @Override
    public void updateImageById(Integer id, String image) {
        imageUpdater.updateImageById(image, new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                UpdateWrapper<ShizhuangCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

                shizhuangCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        });
    }

    @Override
    public List<ShizhuangCategory> selectAll() {
        return shizhuangCategoryMapper.selectList(null);
    }

    @Override
    public List<ShizhuangCategory> selectParent() {
        QueryWrapper<ShizhuangCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("is_parent", 1);
        queryWrapper.isNull("parent_id");

        return shizhuangCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public ShizhuangCategory selectById(Integer id) {
        return shizhuangCategoryMapper.selectById(id);
    }

    @Override
    public List<ShizhuangCategory> selectList(Integer gender) {
        if (gender == null) {
            return new ArrayList<>();
        }

        QueryWrapper<ShizhuangCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.isNull("parent_id");
        queryWrapper.eq("gender", gender);
        queryWrapper.eq("is_parent", 1);

        return shizhuangCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public List<ShizhuangCategory> selectByParentId(Integer parentId) {
        QueryWrapper<ShizhuangCategory> queryWrapper = new QueryWrapper<>();
        
        queryWrapper.eq(
                parentId != null, 
                "parent_id", parentId
        );
        
        return shizhuangCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public Page<ShizhuangCategory> selectByPage(ShizhuangCategoryPager pager) {
        QueryWrapper<ShizhuangCategory> queryWrapper = new QueryWrapper<>();
        Page<ShizhuangCategory> page = Pager.ofPage(pager);

        queryWrapper.like(
                StringUtils.isNotEmpty(pager.getName()),
                "name", pager.getName()
        );
        queryWrapper.eq(
                pager.getType() != null,
                "type", pager.getType()
        );
        queryWrapper.eq(
                pager.getGender() != null,
                "gender", pager.getGender()
        );
        queryWrapper.eq(
                pager.getParentId() != null,
                "parent_id", pager.getParentId()
        );

        return shizhuangCategoryMapper.selectPage(page, queryWrapper);
    }

    public ShizhuangCategory toEntity(ShizhuangCategoryDTO shizhuangCategoryDTO) {
        Integer parentId = shizhuangCategoryDTO.getParentId();
        Integer colorId = shizhuangCategoryDTO.getColorId();

        if (parentId != null) {
            ObjectUtils.requireNonNull(colorId, "时装颜色" + CustomMessages.NOT_NULL);
        }

        ShizhuangCategory shizhuangCategory = new ShizhuangCategory();
        
        shizhuangCategory.setId(shizhuangCategoryDTO.getId());
        shizhuangCategory.setName(shizhuangCategoryDTO.getName());
        shizhuangCategory.setType(shizhuangCategoryDTO.getType());
        shizhuangCategory.setGender(shizhuangCategoryDTO.getGender());
        shizhuangCategory.setColorId(colorId);
        shizhuangCategory.setParentId(parentId);
        shizhuangCategory.setIsParent(parentId == null? 1 : 0);
        
        return shizhuangCategory;
    }

}